package com.ronsai.umtrack.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;

import com.ronsai.umtrack.R;
import com.ronsai.umtrack.api.ObserverResponseListener;
import com.ronsai.umtrack.base.BaseActivity;
import com.ronsai.umtrack.contract.LoginContract;
import com.ronsai.umtrack.model.LoginModel;
import com.ronsai.umtrack.presenter.LoginPresenter;
import com.ronsai.umtrack.util.MacUtil;
import com.ronsai.umtrack.util.ToastUtil;
import com.ronsai.umtrack.util.UUIDS;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.ObservableTransformer;

public class LoginActivity extends BaseActivity<LoginContract.View, LoginContract.Presenter> implements LoginContract.View {

    @BindView(R.id.number_edt)
    EditText numberEdt;
    @BindView(R.id.password_edt)
    EditText passwordEdt;

    private String deviceId;        // 设备唯一标识
    private String imsi;            // imsi
    private String mac;             // mac

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected LoginContract.Presenter createPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    protected LoginContract.View createView() {
        return this;
    }

    @Override
    public <T> ObservableTransformer<T, T> bindLifecycle() {
        return bindToLifecycle();
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        deviceId = UUIDS.buildeID(getApplicationContext()).getDeviceId();
        TelephonyManager mTelephonyMgr = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
            imsi = mTelephonyMgr.getSubscriberId();
        }
        mac = MacUtil.getMac(mContext);
    }

    @OnClick(R.id.login_btn)
    public void onViewClicked() {
        String number = numberEdt.getText().toString();
        String password = passwordEdt.getText().toString();
        if (TextUtils.isEmpty(number)) {
            ToastUtil.showShortToast("请输入账号");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtil.showShortToast("请输入密码");
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("number", number);
        map.put("password", password);
        map.put("deviceType", "1"); // 设备类别 1.安卓 2.ios
        map.put("deviceId", deviceId);
        map.put("mac", mac);
        map.put("imsi", imsi);
        getPresenter().login(map);
    }

    @Override
    public void loginSuccess() {
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}
